//
//  LDSpecialTabView.m
//  Gift
//
//  Created by 李迪 on 15/11/16.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "LDSpecialTabView.h"
#import "LDYuanQuanCell.h"
#import "BaseHeader.h"
#import "ClassificationModel.h"
#import "LDClassYuanQuanDetails.h"

@interface LDSpecialTabView ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain)UITableView *tab;
@property (nonatomic, retain)NSMutableArray *array;
@end

@implementation LDSpecialTabView

-(void)dealloc{
    self.tab.delegate = nil;
    self.tab.dataSource = nil;
    [self.tab release];
    [super dealloc];
}



- (void)craterTab{
    
    self.tab = [[UITableView alloc]initWithFrame:self.view.bounds];
    self.tab.delegate = self;
    self.tab.dataSource = self;
    self.tab.rowHeight = SCREEN_HEIGHT / 4;
    [self.tab registerClass:[LDYuanQuanCell class] forCellReuseIdentifier:@"LDClassYuan"];
    [self.view addSubview:self.tab];
    [self.tab release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LDYuanQuanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LDClassYuan"];
    ClassificationModel *model = [self.array  objectAtIndex:indexPath.row];
    cell.model = model;
    
    return cell;
}

- (void)handleData{
    NSString *str = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/collections/%@/posts?gender=1&generation=1&limit=20&offset=0",self.number];
    [PFAFNetworking getUrl:str body:nil response:PFJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        self.array = [NSMutableArray array];
        
       
        for (NSDictionary *dic in [[resposeObject objectForKey:@"data"]objectForKey:@"posts"]) {
            ClassificationModel *model = [ClassificationModel modelSetDic:dic];
            [_array addObject:model];
        
        }

        [self.tab reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LDClassYuanQuanDetails *details = [[LDClassYuanQuanDetails alloc]init];
    ClassificationModel *model = [self.array objectAtIndex:indexPath.row];
    details.str = model.content_url;
    self.tabBarController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:details animated:YES];
    
    [details release];
}

-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}





- (void)viewDidLoad {
    [super viewDidLoad];
    [self craterTab];
    [self handleData];
    self.title = @"精选";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.], NSForegroundColorAttributeName:[UIColor colorWithRed:1.000 green:1.00 blue:1.000 alpha:1.000]}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
